home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / recurs1a / filedlg.cls < prev    next >
Text File  |  1999-08-27  |  10KB  |  280 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "FileDlg"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  17. Option Explicit
  18. ' FileDlg v1.00 May 1999 contact markb@orionstudios.com
  19. ' Class encapsulating Open and SaveAs common dialogs using API
  20. '   See Object Browser for Property/Method descriptions
  21. '
  22. ' Note minimum (default) Show Method call to get "Open File" name:-
  23. '    Dim FileName As String
  24. '    With New FileDlg
  25. '        If .Show Then
  26. '            FileName .PathFile
  27. '        End If
  28. '    End With
  29. '=================================================================================
  30. '
  31. ' WinAPI Declarations
  32. '
  33. Private Const MAX_PATH = 260
  34. Private Const OFN_EXPLORER = &H80000
  35. Private Const OFN_LONGNAMES = &H200000
  36. Private Const OFN_HIDEREADONLY = &H4
  37. Private Const OFN_NOCHANGEDIR = &H8
  38. Private Const OFN_ENABLEHOOK = &H20 ' With NULL lpfnHook, dialog positions nicely
  39. Private Const OFN_FILEMUSTEXIST = &H1000
  40. Private Const OFN_OVERWRITEPROMPT = &H2
  41. Private Const DEFAULT_FLAGS _
  42.                 = OFN_EXPLORER _
  43.                 Or OFN_LONGNAMES _
  44.                 Or OFN_HIDEREADONLY _
  45.                 Or OFN_NOCHANGEDIR _
  46.                 Or OFN_ENABLEHOOK
  47. Private Const OPEN_FLAGS = DEFAULT_FLAGS Or OFN_FILEMUSTEXIST
  48. Private Const SAVE_FLAGS = DEFAULT_FLAGS Or OFN_OVERWRITEPROMPT
  49.  
  50. Private Type OPENFILENAME
  51.     lStructSize As Long         ' <= Len(mOPENFILENAME)
  52.     hWndOwner As Long           ' <= Owner
  53.     hInstance As Long           ' not used here
  54.     lpstrFilter As String       ' <= BuildFilterString
  55.     lpstrCustomFilter As String ' not used here
  56.     nMaxCustFilter As Long      ' not used here
  57.     nFilterIndex As Long        ' defaults to 1 (first filter)
  58.     lpstrFile As String         ' <= DefaultFileName padded to MAX_PATH, PathFile =>
  59.     nMaxFile As Long            ' <= length of lpstrFile
  60.     lpstrFileTitle As String    ' <= MAX_PATH space reserved, FileTitle =>
  61.     nMaxFileTitle As Long       ' <= length of lpstrFileTitle
  62.     lpstrDefaultDir As String   ' <= DefaultDir (defaults to App.Path)
  63.     lpstrTitle As String        ' <= Title (dialog caption)
  64.     Flags As Long               ' <= OPEN_FLAGS or SAVE_FLAGS constructed constants
  65.     nFileOffset As Integer      ' => position of FileNameOnly in lpstrFile)
  66.     nFileExtension As Integer   ' => position of FileExtOnly in lpstrFile)
  67.     lpstrDefExt As String       ' <= DefaultFileExt
  68.     lCustData As Long           ' not used here
  69.     lpfnHook As Long            ' not used here (see Const OFN_ENABLEHOOK)
  70.     lpTemplateName As String    ' not used here
  71. End Type
  72.  
  73. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  74. Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
  75. Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
  76. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
  77. '
  78. ' Module-level variables
  79. '
  80. Private mOPENFILENAME As OPENFILENAME
  81. Private mFilters As VBA.Collection
  82. '
  83. ' Local variables to hold property values
  84. '
  85. Private mvarOwner As Long
  86. Private mvarDefaultDir As String
  87. Private mvarDefaultFileName As String
  88. Private mvarDefaultFileExt As String
  89. Private mvarTitle As String
  90. Private mvarPathFile As String
  91. Private mvarPath As String
  92. Private mvarFileName As String
  93. Private mvarFileNameOnly As String
  94. Private mvarFileExtOnly As String
  95. Private mvarFileTitle As String
  96. Private mvarPathFileShort As String ' relevant to OpenDialog only
  97. '
  98. ' Enumerations
  99. '
  100. Public Enum DialogType
  101.     OpenDialog
  102.     SaveAsDialog
  103. End Enum
  104. '
  105. ' Write-only Properties
  106. '
  107. Public Property Let Owner(ByVal vData As Long)
  108. Attribute Owner.VB_Description = "Window handle of calling form.  (e.g., Form1.hWnd)"
  109.     mvarOwner = vData
  110. End Property
  111.  
  112. Public Property Let DefaultDir(ByVal vData As String)
  113. Attribute DefaultDir.VB_Description = "Initial file directory. Defaults to application path."
  114.     mvarDefaultDir = vData
  115. End Property
  116.  
  117. Public Property Let DefaultFileName(ByVal vData As String)
  118. Attribute DefaultFileName.VB_Description = "Filename used to initialize the File Name edit control."
  119.     mvarDefaultFileName = vData
  120. End Property
  121.  
  122. Public Property Let DefaultFileExt(ByVal vData As String)
  123. Attribute DefaultFileExt.VB_Description = "Extension appended to filename if user does not supply one. Only the first 3 characters are appended. A period (.) MUST NOT be included."
  124.     mvarDefaultFileExt = vData
  125. End Property
  126.  
  127. Public Property Let Title(ByVal vData As String)
  128. Attribute Title.VB_Description = "Window caption for dialog."
  129.     mvarTitle = vData
  130. End Property
  131. '
  132. ' Read-only Properties
  133. '
  134.  
  135. Public Property Get PathFile() As String
  136. Attribute PathFile.VB_Description = "Full Path (WITH FileName)"
  137.     PathFile = mvarPathFile
  138. End Property
  139.  
  140. Public Property Get Path() As String
  141. Attribute Path.VB_Description = "Path only (WITHOUT filename)."
  142.     Path = mvarPath
  143. End Property
  144.  
  145. Public Property Get FileName() As String
  146. Attribute FileName.VB_Description = "Filename WITH extension."
  147.     FileName = mvarFileName
  148. End Property
  149.  
  150. Public Property Get FileNameOnly() As String
  151. Attribute FileNameOnly.VB_Description = "Filename WITHOUT extension."
  152.     FileNameOnly = mvarFileNameOnly
  153. End Property
  154.  
  155. Public Property Get FileExtOnly() As String
  156. Attribute FileExtOnly.VB_Description = "File extension (does NOT include a period(.)."
  157.     FileExtOnly = mvarFileExtOnly
  158. End Property
  159.  
  160. Public Property Get FileTitle() As String
  161. Attribute FileTitle.VB_Description = "File title for display purposes only. Reflects user  preferences."
  162.     FileTitle = mvarFileTitle
  163. End Property
  164.  
  165. Public Property Get PathFileShort() As String
  166. Attribute PathFileShort.VB_Description = "Short verion of PathFile (including 8.3 style filename)."
  167.     PathFileShort = mvarPathFileShort
  168. End Property
  169. '
  170. ' Methods
  171. '
  172. Public Function Show(Optional DlgType As DialogType = OpenDialog) As Boolean
  173. Attribute Show.VB_Description = "Show modal dialog for ""Openf file"" or ""Save file"" (see dlgtype enumeration). Returns True if a file name available."
  174. Attribute Show.VB_UserMemId = 0
  175.     
  176.     On Error GoTo Show_Error
  177.     
  178.     Dim Result As Boolean    ' default function result = false
  179.     Dim boolResult As Long
  180.     Dim lngResult As Long
  181.     Dim strShortName As String
  182.     Dim lngFilePos As Long
  183.     
  184.     With mOPENFILENAME
  185.         .lStructSize = Len(mOPENFILENAME)
  186.         .hWndOwner = mvarOwner
  187.         .lpstrFilter = BuildFilterString
  188.         .lpstrFile = mvarDefaultFileName & String$(MAX_PATH - Len(mvarDefaultFileName), vbNullChar)
  189.         .nMaxFile = Len(.lpstrFile)
  190.         .lpstrTitle = mvarTitle
  191.         .lpstrFileTitle = Space$(MAX_PATH)
  192.         .nMaxFileTitle = Len(.lpstrFileTitle)
  193.         .lpstrDefaultDir = mvarDefaultDir
  194.         .lpstrDefExt = mvarDefaultFileExt
  195.     End With
  196.     
  197.     Select Case DlgType
  198.         Case OpenDialog
  199.             mOPENFILENAME.Flags = OPEN_FLAGS
  200.             boolResult = GetOpenFileName(mOPENFILENAME)
  201.         Case SaveAsDialog
  202.             mOPENFILENAME.Flags = SAVE_FLAGS
  203.             boolResult = GetSaveFileName(mOPENFILENAME)
  204.     End Select
  205.     
  206.     If boolResult Then
  207.         With mOPENFILENAME
  208.             mvarPathFile = TrimStr(.lpstrFile)
  209.             mvarPath = Left$(mvarPathFile, .nFileOffset - 1)
  210.             lngFilePos = .nFileOffset + 1
  211.             mvarFileName = Mid$(mvarPathFile, lngFilePos)
  212.             mvarFileNameOnly = Mid$(mvarPathFile, lngFilePos, .nFileExtension - lngFilePos)
  213.